﻿using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Tokens;

namespace dotnet.util
{
    public class JWTUtil
    {
        public static readonly string Key = "11111111111111111111111111111111";

        public static readonly string Issuer = "cz";

        public static readonly string Audience = "cz";

        public  static string GenerateJwtToken(string userName)
        {

            var tokenHander = new JwtSecurityTokenHandler();
            var key = Encoding.ASCII.GetBytes(JWTUtil.Key);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                // configure the custome information in the Claim instance
                Subject = new System.Security.Claims.ClaimsIdentity([new Claim("name", userName)]),

                // configure the expiration time
                Expires = DateTime.UtcNow.AddHours(1),

                // issuer could be your service name or other, and it will be the [iss] of the token
                Issuer = JWTUtil.Issuer,

                // audience could be the audience of your service, and it will be the [aud] of the token
                Audience = JWTUtil.Audience,

                // configure secret algorithm and secret key for signature part
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)

            };

            var token = tokenHander.CreateToken(tokenDescriptor);
            return tokenHander.WriteToken(token);
        }
    }
}
